<template>
    <div class="fund_account_form_container">
        <el-form ref="baseFundAccountForm" :model="formData" :rules="rules" label-width="8em">
            <el-form-item label="账号名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入账号名称" clearable />
            </el-form-item>
            <el-form-item label="科目代码" prop="subject_code">
                <el-input v-model="formData.subject_code" placeholder="请输入科目代码" clearable />
            </el-form-item>
            <el-form-item label="账号类型" prop="account_type">
                <el-radio-group v-model="formData.account_type">
                    <el-radio :label="1">现金</el-radio>
                    <el-radio :label="2">银行</el-radio>
                </el-radio-group>
            </el-form-item>
            <template v-if='formData.account_type === 2'>
                <el-form-item label="银行名称" prop="bank_name">
                    <el-input v-model="formData.bank_name" placeholder="请输入银行名称" clearable />
                </el-form-item>
                <el-form-item label="银行账号" prop="bank_account">
                    <el-input v-model="formData.bank_account" v-int-new placeholder="请输入银行账号" clearable />
                </el-form-item>
                <el-form-item label="开户名" prop="account_name">
                    <el-input v-model="formData.account_name" placeholder="请输入开户名" clearable />
                </el-form-item>
            </template>
            <el-form-item label="期初金额" prop="init_amount">
                <el-input v-model="formData.init_amount" autocomplete="off" clearable
                          v-double-input="{ len: 4, negative: false }"/>
            </el-form-item>
            <el-form-item label="备注" prop="note_info">
                <el-input type="textarea" v-model="formData.note_info"
                          placeholder="请输入备注" maxlength="200" show-word-limit clearable />
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "FundAccountForm",
    props: {
        options: {
            type: Object,
            default: null,
        },
    },
    data() {
        return {
            formData: {},
            rules: {
                name: [{ required: true, message: '请输入账号名称', trigger: 'blur' }],
                account_type: [{ required: true, message: '请选择账号类型', trigger: 'blur' }],
                bank_name: [{ required: true, message: '请输入银行名称', trigger: 'blur' }],
                bank_account: [{ required: true, message: '请输入银行账号', trigger: 'blur' }],
                account_name: [{ required: true, message: '请输入开户名', trigger: 'blur' }],
                // subject_code: [{ required: true, message: '请输入科目代码', trigger: 'blur' }],
            },
        }
    },
    watch: {
        options: {
            handler() {
                this.init();
            },
            immediate: true,
            deep: true,
        },
    },
    methods: {
        init() {
            const _this = this;
            if (!_this.options) {
				return;
            }
	        _this.formData = $.extend({}, {account_type: 1}, (_this.options.row || {}));
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('baseFundAccountForm');
            if (!valid) {
                return false;
            }
            let subData = $.extend({}, _this.formData);
            subData.owner_sid = -1;
            return _this.fetchData(_this.$api.API_SaveBaseFundAccount, subData).then(result => {
                _this.$message.success('保存成功！');
	            _this.$emit('event', {type: 'refresh', data: result.data });
                return true;
            }).catch(() => {
                return false;
            });
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.fund_account_form_container {

}
</style>
